import { message } from 'antd';
import moment from 'moment';
import {
  getCustTotal,
  getEmpTotal,
  getEmpChart,
  getEmpOrder,
  getFeeChart,
  getCustFeeOrder,
  getSuppFeeOrder,
  getAddRedEmpChart,
  getAddEmpOrder,
  getRedEmpOrder,
} from '@/services/analysiscs';

export default {
  state: {
    custNum: { staNum: 0, addNum: 0, delNum: 0 },
    empNum: { staNum: 0, addNum: 0, delNum: 0 },
    // 服务人数
    empData: [],
    empMonth: moment(),
    empOrderData: [],
    // 服务费
    feeData: [],
    feeMonth: [moment().subtract(11, 'months'), moment()],
    feeStartTime: moment()
      .subtract(11, 'months')
      .format('YYYY-MM'),
    feeEndTime: moment().format('YYYY-MM'),
    feeActiveKey: '1',
    custFeeData: [],
    suppFeeData: [],

    // 增减员
    addRedEmpData: [],
    addRedEmpMonth: [moment().subtract(11, 'months'), moment()],
    addRedEmpStartTime: moment()
      .subtract(11, 'months')
      .format('YYYY-MM'),
    addRedEmpEndTime: moment().format('YYYY-MM'),
    addRedEmpActiveKey: '1',
    addEmpData: [],
    redEmpData: [],
  },
  effects: {
    // 获取客户总量
    *fetchCustTotal({ payload }, { call, put }) {
      const response = yield call(getCustTotal, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          custNum: 0,
        },
      });
      if (response.success && response.data) {
        yield put({
          type: 'updateModelData',
          payload: {
            custNum: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取客户数据：服务端错误');
      }
    },
    // 获取服务人数总量
    *fetchEmpTotal({ payload }, { call, put }) {
      const response = yield call(getEmpTotal, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          empNum: 0,
        },
      });
      if (response.success && response.data) {
        yield put({
          type: 'updateModelData',
          payload: {
            empNum: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取服务人员数据：服务端错误');
      }
    },
    // 服务人数趋势图
    *fetchEmpChart({ payload }, { call, put }) {
      yield put({
        type: 'updateModelData',
        payload: {
          empData: [],
        },
      });
      const response = yield call(getEmpChart, payload);
      if (response.success) {
        if (response.data) {
          const entrData = [];
          response.data.forEach(item => {
            entrData.push({
              type: '委托：',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.entrNum || 0,
            });
          });
          const dispData = [];
          response.data.forEach(item => {
            dispData.push({
              type: '派遣：',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.dispNum || 0,
            });
          });
          const outData = [];
          response.data.forEach(item => {
            outData.push({
              type: '外包：',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.outNum || 0,
            });
          });
          yield put({
            type: 'updateModelData',
            payload: {
              empData: [...entrData, ...dispData, ...outData],
            },
          });
        } else {
          message.warning('该月份下未查询到服务人员数据！');
        }
      } else {
        message.warning(response.message || '获取服务人员图表数据：服务端错误');
      }
    },
    //  服务人数排行
    *fetchEmpOrder({ payload }, { call, put }) {
      const response = yield call(getEmpOrder, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          empOrderData: [],
        },
      });
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'updateModelData',
            payload: {
              empOrderData: response.data,
            },
          });
        } else {
          message.warning('该月份下未查询到服务人员数据！');
        }
      } else {
        message.warning(response.message || '获取客户服务人数排行数据：服务端错误');
      }
    },
    // 服务费趋势图
    *fetchFeeChart({ payload }, { call, put }) {
      yield put({
        type: 'updateModelData',
        payload: {
          feeData: [],
        },
      });
      const response = yield call(getFeeChart, payload);
      if (response.success) {
        if (response.data) {
          const custFeeData = [];
          response.data.forEach(item => {
            custFeeData.push({
              type: '自主开发收费：',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.custFee || 0,
            });
          });
          const suppFeeData = [];
          response.data.forEach(item => {
            suppFeeData.push({
              type: '线下委托收费：',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.suppFee || 0,
            });
          });
          yield put({
            type: 'updateModelData',
            payload: {
              feeData: [...custFeeData, ...suppFeeData],
            },
          });
        } else {
          message.warning('该月份下未查询到服务费数据！');
        }
      } else {
        message.warning(response.message || '获取服务费图表数据：服务端错误');
      }
    },
    //  自主开发排行:客户
    *fetchCustFeeOrder({ payload }, { call, put }) {
      const response = yield call(getCustFeeOrder, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          custFeeData: [],
        },
      });
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'updateModelData',
            payload: {
              custFeeData: response.data,
            },
          });
        } else {
          message.warning('该月份下未查询到服务费数据！');
        }
      } else {
        message.warning(response.message || '获取自主开发收费排行数据：服务端错误');
      }
    },
    // 线下委托排行：供应商
    *fetchSuppFeeOrder({ payload }, { call, put }) {
      const response = yield call(getSuppFeeOrder, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          suppFeeData: [],
        },
      });
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'updateModelData',
            payload: {
              suppFeeData: response.data,
            },
          });
        } else {
          message.warning('该月份下未查询到服务费数据！');
        }
      } else {
        message.warning(response.message || '获取线下委托收费排行数据：服务端错误');
      }
    },
    // 获取增减员趋势图
    *fetchAddRedEmpChart({ payload }, { call, put }) {
      yield put({
        type: 'updateModelData',
        payload: {
          addRedEmpData: [],
        },
      });
      const response = yield call(getAddRedEmpChart, payload);
      if (response.success) {
        if (response.data) {
          const addEmpData = [];
          response.data.forEach(item => {
            addEmpData.push({
              type: '增员',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.addNum || 0,
            });
          });
          const redEmpData = [];
          response.data.forEach(item => {
            redEmpData.push({
              type: '减员',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.delNum || 0,
            });
          });
          yield put({
            type: 'updateModelData',
            payload: {
              addRedEmpData: [...addEmpData, ...redEmpData],
            },
          });
        } else {
          message.warning('该月份下未查询到增减员数据！');
        }
      } else {
        message.warning(response.message || '获取增减员图表数据：服务端错误');
      }
    },
    // 获取增员排行榜
    *fetchAddEmpOrder({ payload }, { call, put }) {
      const response = yield call(getAddEmpOrder, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          addEmpData: [],
        },
      });
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'updateModelData',
            payload: {
              addEmpData: response.data,
            },
          });
        } else {
          message.warning('该月份下未查询到增员数据！');
        }
      } else {
        message.warning(response.message || '获取增员排行数据：服务端错误');
      }
    },
    // 获取减员排行榜
    *fetchRedEmpOrder({ payload }, { call, put }) {
      const response = yield call(getRedEmpOrder, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          redEmpData: [],
        },
      });
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'updateModelData',
            payload: {
              redEmpData: response.data,
            },
          });
        } else {
          message.warning('该月份下未查询到减员数据！');
        }
      } else {
        message.warning(response.message || '获取减员排行数据：服务端错误');
      }
    },
  },
  reducers: {
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
